package com.zhiyun.yycall.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.zhiyun.common.YYCallUtils;
import com.zhiyun.message.ResetPwdByVerifyRequest;
import com.zhiyun.message.ResetPwdByVerifyResponse;
import com.zhiyun.net.AsynCallback;
import com.zhiyun.net.Net;
import com.zhiyun.net.Response;
import com.zhiyun.trace.TraceActivity;
import com.zhiyun.yycall.R;

public class ResetPwdActivity extends TraceActivity {

	private EditText verifyCodeView;
	private EditText passwordView;
	private String phoneNo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.resetpwd_activity);
		verifyCodeView = (EditText) this.findViewById(R.id.verify_code);
		passwordView = (EditText) this.findViewById(R.id.password);

		phoneNo = this.getIntent().getStringExtra("phoneNo");
		((TextView) this.findViewById(R.id.phoneNo)).setText(phoneNo);

		this.findViewById(R.id.reset_btn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {

						if (StringUtils.isBlank(passwordView.getText()
								.toString())) {
							AndroidUtils.toastShort("请输入新密码");
							return;

						}

						if (StringUtils.isBlank(verifyCodeView.getText()
								.toString())) {
							AndroidUtils.toastShort("短信验证码不能为空");
							return;

						}

						remoteResetPwd();

					}
				});

	}

	private void remoteResetPwd() {

		final String password = passwordView.getText().toString();
		String verifyCode = verifyCodeView.getText().toString();

		ResetPwdByVerifyRequest req = new ResetPwdByVerifyRequest();
		req.setPhoneNo(phoneNo);
		req.setPassword(YYCallUtils.encryptPwd(phoneNo, password));
		req.setVerifyCode(verifyCode);

		Net.doHttpRemote(this, req, new ResetPwdByVerifyResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {
							AndroidUtils.toastLong("密码重置失败，原因:" + r.getError());
							return false;

						}

						// 密码重置成功
						Intent data = new Intent();
						data.putExtra("password", password);
						setResult(RESULT_OK, data);
						finish();
						AndroidUtils.toastShort("密码重置成功。");

						return false;
					}
				});

	}
}
